package name.ekr.assetguide.data;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import name.ekr.assetguide.domain.AssetsPrice;

public class DataProvider {

    private List<DataRecord> internalData = new LinkedList<DataRecord>();
    private List<DataRecord> externalData;

	/**
	 * Contract:
	 * - Pre-condition: {Records ordered by Date ASC}
	 */
	public DataProvider(String file) throws IOException, ParseException {
		BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
		String currentLine = null;
		String[] stringParts = null;
		SimpleDateFormat formatter = new SimpleDateFormat("dd.mm.yyyy");
		Date date = null;
		
		in.readLine(); //omit header
		while( (currentLine = in.readLine()) != null ) {
			stringParts = currentLine.split("\t");
			
			date = formatter.parse(stringParts[0]);
			AssetsPrice prices = new AssetsPrice(
					Double.parseDouble(stringParts[1]),
					Double.parseDouble(stringParts[2]),
					Double.parseDouble(stringParts[3]), 
					Double.parseDouble(stringParts[4]), 
					Double.parseDouble(stringParts[5]),
					Double.parseDouble(stringParts[6])
				);
			
			internalData.add(new DataRecord(date, prices));
		}
		
		externalData = Collections.unmodifiableList(internalData);
	}
	
	public List<DataRecord> getData() {
		return externalData;
	}

}









